package com.zbb.order.api;


import com.zbb.base.model.Result;
import com.zbb.order.feignclient.UserServiceClient;
import com.zbb.order.model.po.User;
import com.zbb.order.service.ExitUserInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Slf4j
@RestController
@Api(value = "操作用户信息",tags = "查询用户信息/更改用户信息")
public class ExitUserInfoController {

    @Autowired
    UserServiceClient userServiceClient;

    @Autowired
    ExitUserInfoService exitUserInfoService;

    @ApiOperation("用户信息更改")
    @PostMapping("/updateUser")
    public Result updateUser(@RequestBody User user){
        Result result = exitUserInfoService.updateUser(user);
        return Result.ok();
    }

    @ApiOperation("获取用户信息")
    @GetMapping("request/userInfo")
    public Result userInfo(){
        User user = exitUserInfoService.getUserInfo();
        return Result.ok(user);
    }

    @ApiOperation("退出登录")
    @GetMapping("/hand/logout/{userId}")
    public Result doLogout(HttpServletRequest request, HttpServletResponse response, @PathVariable String userId) {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication != null) {//清除认证
            new SecurityContextLogoutHandler().logout(request, response, authentication);
        }
        exitUserInfoService.doLogout(userId);
        return Result.ok();
    }



}
